package com.itheima.threadtest2;

/**
 *
 */
public class MyRunnable implements Runnable{

    static int count = 100 ;    //礼品的数量初始值为 100

    static Object lock = new Object();
    /**
     *
     */
    @Override
    public void run() {

        while(true) {
            synchronized (com.itheima.a10threadsafe2.MyRunnable.class) {
                if (count >= 10) {
                    try {
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName() + "正在发礼物 ， 还剩：" + count -- + "件");
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    //共享变量小于 10
                    break ;
                }
            }
        }
    }
}
